using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using XFGameFramework.TweenSystem;

namespace XFGameFramework
{

    [Serializable]
    public class AnimationDataGroup 
    { 
        public List<AnimationData> animationDatas;

        public List<Tweener> tweeners;

        public float Duration 
        {
            get 
            {
                float duration = 0;

                foreach (var animationData in animationDatas) 
                {
                    if(animationData.duration > duration)
                        duration = animationData.duration;
                }

                return duration;
            }
        }

        public void Play(bool ingoreTimeScale) 
        {

            if (tweeners == null)
                tweeners = new List<Tweener>();

            tweeners.Clear();

            foreach (var animationData in animationDatas) 
            {
                animationData.HandleAnimationData(false, false, ingoreTimeScale);

                if(animationData.tweener != null && animationData.target != null) 
                    tweeners.Add(animationData.tweener); 
            }
             
        }

    }


    public class TweenAnimationSequence : MonoBehaviour
    {

        #region 字段

        [SerializeField]
        [Tooltip("所有序列动画数据")]
        [Header("所有序列动画数据")]
        private List<AnimationDataGroup> sequences;

        [SerializeField]
        [Tooltip("循环模式")]
        [Header("循环模式")]
        private LoopMode loopMode;

        [SerializeField]
        [Tooltip("循环次数(<=0代表一直循环)")]
        [Header("循环次数(<=0代表一直循环)")]
        private int loopCount;

        [SerializeField]
        [Tooltip("是否自动播放")]
        [Header("是否自动播放")]
        private bool playOnAwake = true;

        private TweenerSequence sequence = null;
         
        [SerializeField]
        [Tooltip("是否忽略时间缩放,默认:true!")] 
        [Header("是否忽略时间缩放,默认:true!")]
        private bool ingoreTimeScale = true;


        #endregion

        #region 属性

        public List<AnimationDataGroup> Sequences => sequences;


        public bool IngoreTimeScale
        {
            get { 
                return ingoreTimeScale;
            }
            set { 
                ingoreTimeScale= value;
            }
        }

        #endregion

        #region 事件

        [Tooltip("序列动画播放完成事件")]
        [Header("序列动画播放完成事件")]
        public UnityEvent onCompleted;

        [Tooltip("序列动画中断事件")]
        [Header("序列动画中断事件")]
        public UnityEvent onKill;

        #endregion



        #region 方法

        public void Play() 
        { 
            sequence.Clear();

            for (int i = 0; i < sequences.Count; i++)
            {
                sequences[i].Play(ingoreTimeScale);

                if (sequences[i].tweeners == null || sequences[i].tweeners.Count == 0)
                {
                    if(sequences[i].Duration > 0 )
                        sequence.AppendInterval(sequences[i].Duration);
                }
                else
                { 
                    sequence.Append(sequences[i].tweeners);
                }
            }
            
            sequence.Play();
        }

        [Obsolete("已过时,请使用方法:Kill代替!")]
        public void Stop() { 
            sequence.Stop();
        }

        public void Kill() 
        {
            sequence.Kill();
        }


        public void Pause(bool pause) 
        {
            sequence.Pause(pause);
        }


        private void OnCompleted(TweenerSequence s)
        { 
            onCompleted?.Invoke();
        }

        private void OnKill(TweenerSequence s)
        {
            onKill?.Invoke();
        }
         
        #endregion


        #region 生命周期

        private void Awake()
        {
            if (sequence == null)
                sequence = new TweenerSequence();

            sequence.AddCompleteEvent(OnCompleted);
            sequence.onKill += OnKill;
        }

        private void OnEnable()
        {
            if(playOnAwake)
                Play();
        }

        private void OnDisable()
        {
            Kill();
        }

        #endregion

    }

}

